package com.rongshu.view.home.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.home.adapter.ArticleDetailsRecomendAdapter;
import com.rongshu.view.home.contract.LookMoreRecommendView;
import com.rongshu.view.home.entity.PeopleRecommendRespose;
import com.rongshu.view.home.entity.RecomendMsg;
import com.rongshu.view.home.presenter.LookMoreRecommendPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class LookMoreRecommendActivity extends BaseActivity<LookMoreRecommendPresenter> implements LookMoreRecommendView {
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private ArticleDetailsRecomendAdapter adapter = null;
    private int pageIndex = 1;
    private int pageSize = 10;
    //map
    private Map<String, Object> map = null;
    //当前文章ID,(1文章，2连载),推荐人ID
    private int contribute_id, contribute_type;
    //refresh,load
    private boolean isrefresh = true;

    @Override
    protected int initLayout() {
        return R.layout.activity_article_recommend_view;
    }

    @Override
    protected void initView() {
        //获取意图数据
        Intent intent = getIntent();
        contribute_id = intent.getIntExtra(Constant.INTENT_DATA, 0);
        contribute_type = intent.getIntExtra(Constant.INTENT_MESSAGE, 0);
        //set presenter
        setPresenter(new LookMoreRecommendPresenter(this), this);
        //manager
        manager = new LinearLayoutManager(this);
        //adapter
        adapter = new ArticleDetailsRecomendAdapter(this);
        //recyclerview
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        //refresh
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                isrefresh = true;
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                getMoreRecommendMsg();
            }
        });

        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                isrefresh = false;
                pageIndex++;
                getMoreRecommendMsg();
            }
        });
        smartRefreshLayout.autoRefresh();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getMoreRecommendMsg();
    }

    /**
     * 获取更多推荐语
     */
    private void getMoreRecommendMsg() {
        map = new HashMap<>();
        map.put("contribute_id", contribute_id);
        map.put("contribute_type", contribute_type);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        presenter.getMoreRecommendMsg(contribute_id, contribute_type,
                pageIndex, pageSize, Common.encryptionSign(map));
        map.clear();
    }


    @OnClick({R.id.iv_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
        }
    }

    @Override
    public void onLookMoreRecommend(List<RecomendMsg.ResponseBean> responseBeanList) {
        if (adapter != null) {
            if (isrefresh) {
                smartRefreshLayout.finishRefresh();
                adapter.setResponseBeanList(responseBeanList);
                adapter.notifyDataSetChanged();
            } else {
                if (responseBeanList == null || responseBeanList.isEmpty()) {
                    smartRefreshLayout.finishLoadmoreWithNoMoreData();
                } else {
                    smartRefreshLayout.finishLoadmore();
                    adapter.addResponseBeanList(responseBeanList);
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }
}
